package com.echat.ebluetooth;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.echat.ebluetooth.util.PreferencesUtils;

import static com.echat.ebluetooth.constant.Constants.DEFAULT_SCREEN;
import static com.echat.ebluetooth.constant.Constants.DEFAULT_SERVER_HOST;
import static com.echat.ebluetooth.constant.Constants.DEFAULT_SERVER_PORT;
import static com.echat.ebluetooth.constant.Constants.KEY_ACCOUNT;
import static com.echat.ebluetooth.constant.Constants.KEY_HOST;
import static com.echat.ebluetooth.constant.Constants.KEY_PORT;
import static com.echat.ebluetooth.constant.Constants.KEY_SAVE;
import static com.echat.ebluetooth.constant.Constants.KEY_SCREEN;

public class SettingActivity extends AppCompatActivity {

    private EditText etHost;
    private EditText etPort;
    private EditText etAccount;
    private EditText etScreen;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        setText();
    }

    private void initView() {
        etHost = (EditText) findViewById(R.id.et_host);
        etPort = (EditText) findViewById(R.id.et_port);
        etAccount = (EditText) findViewById(R.id.et_account);
        etScreen = (EditText) findViewById(R.id.et_screen);
        btnSave = (Button) findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInput()) {
                    Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private void setText() {
        String host = (String) PreferencesUtils.get(this, KEY_HOST, DEFAULT_SERVER_HOST);
        String port = (String) PreferencesUtils.get(this, KEY_PORT, String.valueOf(DEFAULT_SERVER_PORT));
        String account = (String) PreferencesUtils.get(this, KEY_ACCOUNT, "");
        String screen = (String) PreferencesUtils.get(this, KEY_SCREEN, DEFAULT_SCREEN);
        etHost.setText(host);
        etPort.setText(port);
        etAccount.setText(TextUtils.isEmpty(account) ? "" : account);
        etScreen.setText(screen);
    }

    private boolean checkInput() {
        boolean inputCorrect = false;
        String content;
        String host = etHost.getText().toString().trim();
        String port = etPort.getText().toString().trim();
        String account = etAccount.getText().toString().trim();
        String screen = etScreen.getText().toString().trim();
        if (TextUtils.isEmpty(host)) {
            content = "服务器IP为空，请填写";
        } else if (TextUtils.isEmpty(port)) {
            content = "端口号为空，请填写";
        } else if (TextUtils.isEmpty(account)) {
            content = "定位账号为空，请填写";
        } else {
            inputCorrect = true;
            content = "保存成功";
            saveConfig(host, port, account, screen);
        }
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
        return inputCorrect;
    }

    private void saveConfig(String host, String port, String account, String screen) {
        PreferencesUtils.put(this, KEY_HOST, host);
        PreferencesUtils.put(this, KEY_PORT, port);
        PreferencesUtils.put(this, KEY_ACCOUNT, account);
        PreferencesUtils.put(this, KEY_SCREEN, screen);
        PreferencesUtils.put(this, KEY_SAVE, true);
    }
}
